package com.zzq.calcgame.storage;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.zzq.calcgame.entity.Operator;
import com.zzq.calcgame.entity.Question;

import java.util.ArrayList;

/**
 * Created by android on 16/9/29.
 */

public class QuestionDao {

    CCOpenHelper dbHelper;

    public QuestionDao(Context ctx){
        dbHelper=new CCOpenHelper(ctx);
    }

    /**
     *      * +ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
     +PAPER_ID + " INTEGER,"
     +NUMBER_1 + " TEXT,"
     +NUMBER_2 +" TEXT,"
     +ANSWER +" INTEGER,"
     +OPERATOR+ " INTEGER,"
     * @param questions
     * @return
     */
    public boolean batchInsert(ArrayList<Question> questions){
        boolean retValue=true;
        try{
            SQLiteDatabase db=dbHelper.getWritableDatabase();
            SQLiteStatement statement=db.compileStatement("INSERT INTO "+ CCDb.QuestionTable.TABLE_NAME+
                    "("+ CCDb.QuestionTable.PAPER_ID+","+ CCDb.QuestionTable.NUMBER_1+","+ CCDb.QuestionTable.NUMBER_2+","+
                    CCDb.QuestionTable.ANSWER+","+ CCDb.QuestionTable.OPERATOR+") VALUES(?,?,?,?,?)");
            db.beginTransaction();
            Question question;
            for(int i=0;i<questions.size();i++){
                question=questions.get(i);
                statement.bindAllArgsAsStrings(new String[]{String.valueOf(question.getPaperId()),
                        question.getNumber1(),question.getNumber2(),question.getAnswer(),
                        String.valueOf(question.getOperator().getType())});
                statement.execute();
                statement.clearBindings();
            }
            db.setTransactionSuccessful();
            db.endTransaction();
        }catch (Exception e){
            retValue=false;
        }
        return retValue;
    }


    public int batchUpdate(ArrayList<Question> questions){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        SQLiteStatement statement=db.compileStatement("UPDATE "+ CCDb.QuestionTable.TABLE_NAME+
                "("+ CCDb.QuestionTable.PAPER_ID+","+ CCDb.QuestionTable.NUMBER_1+","+ CCDb.QuestionTable.NUMBER_2+
                CCDb.QuestionTable.ANSWER+","+ CCDb.QuestionTable.OPERATOR+") VALUES(?,?,?,?,?)");
        db.beginTransaction();
        Question question;
        for(int i=0;i<questions.size();i++){
            question=questions.get(i);
            statement.bindAllArgsAsStrings(new String[]{String.valueOf(question.getPaperId()),
                    question.getNumber1(),question.getNumber2(),question.getAnswer(),
                    String.valueOf(question.getOperator().getType())});
            statement.clearBindings();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        return 0;
    }


    /**
     * 插入一条数据
     * @param question
     * @return
     */
    public int insert(Question question){
        int retValue=-1;
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(CCDb.QuestionTable.PAPER_ID,question.getPaperId());
        values.put(CCDb.QuestionTable.NUMBER_1,question.getNumber1());
        values.put(CCDb.QuestionTable.NUMBER_2,question.getNumber2());
        values.put(CCDb.QuestionTable.ANSWER,question.getAnswer());
        values.put(CCDb.QuestionTable.OPERATOR,question.getOperator().getType());
        if(db.insert(CCDb.QuestionTable.TABLE_NAME,null,values)!=0){
            Cursor cursor=db.rawQuery("select last_insert_rowid() from "+ CCDb.QuestionTable.TABLE_NAME,null);
            if(cursor.moveToFirst()){
                retValue=cursor.getInt(0);
            }
        }
        return retValue;
    }

    /**
     * +ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
     +PAPER_ID + " INTEGER,"
     +NUMBER_1 + " TEXT,"
     +NUMBER_2 +" TEXT,"
     +ANSWER +" INTEGER,"
     +OPERATOR+ " INTEGER,"
     * @param paperId
     * @return
     */
    public ArrayList<Question> getQuestionByPaperId(int paperId){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from " + CCDb.QuestionTable.TABLE_NAME+" where "+ CCDb.QuestionTable.PAPER_ID+" = ?",new String[]{String.valueOf(paperId)});
        ArrayList<Question> questions=new ArrayList<>();
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                Question item=new Question();
            item.setId(cursor.getInt(0));
            item.setPaperId(cursor.getInt(1));
            item.setNumber1(cursor.getString(2));
            item.setNumber2(cursor.getString(3));
            item.setAnswer(cursor.getString(4));
            item.setOperator(Operator.getOperator(cursor.getInt(5)));
            questions.add(item);
        }
        if(!cursor.isClosed()){
            cursor.close();
        }
        db.close();

        return questions;
    }

    public ArrayList<Question> getAllQuestion(){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from " + CCDb.QuestionTable.TABLE_NAME,null);
        ArrayList<Question> questions=new ArrayList<>();
        for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
            Question item=new Question();
            item.setId(cursor.getInt(0));
            item.setPaperId(cursor.getInt(1));
            item.setNumber1(cursor.getString(2));
            item.setNumber2(cursor.getString(3));
            item.setAnswer(cursor.getString(4));
            item.setOperator(Operator.getOperator(cursor.getInt(5)));
            questions.add(item);
        }
        if(!cursor.isClosed()){
            cursor.close();
        }
        db.close();

        return questions;
    }
}
